package com.wei.czz.common.utils;

import java.nio.ByteBuffer;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2025-04-24 23:45:01
 * className: FileUtils
 * version: 1.0
 * description:
 */
public class FileUtils {

    /**
     * 动态计算缓冲区大小(8KB-1MB之间)
     * @param fileSize 文件大小
     * @return 缓冲区大小
     */
    public static ByteBuffer dinamicByteBuffer(long fileSize) {
        if (fileSize < 1024 * 1024) {
            // 如果文件大小小于1MB，动态计算缓冲区大小(8KB-64KB之间)
            int bufferSize = (int) Math.min(64 * 1024, Math.max(8 * 1024, fileSize / 100));
            return ByteBuffer.allocate(bufferSize);
        } else {
            // 如果文件大小大于1MB，动态计算缓冲区大小(8KB-1MB之间)
            int bufferSize = (int) Math.min(1024 * 1024, Math.max(8 * 1024, fileSize / 100));
            return ByteBuffer.allocateDirect(bufferSize);
        }
    }
}
